<?php

    function caLikes($likes, $dislikes) {
        return $likes - $dislikes;
    }
    
    function getThumbs($users) {
        //Genre: 0, Director: 1, Writer: 2, Actor: 3
        
        $thumbs_data = array(0 => array(), 1 => array(), 2 => array(), 3 => array());
        try {
            $conn = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME, DB_USER, DB_PASS);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            
            $userlist = "";
            foreach ($users as $user) {
                $userlist .= $user . ',';
            }
            $userlist = substr_replace($userlist ,"", -1);
            $stmt = $conn->prepare("select likes, cnt - likes as dislikes, type, entity from (
                                    select count(*) as cnt, sum(rank) as likes, type, entity, rank from thumbs where userid in ($userlist)
                                    group by type, entity) t;");
            $stmt->execute();
            
            while ($thumbs_row = $stmt->fetch()) {
                $type = $thumbs_row['type'];
                $likes = $thumbs_row['likes'];
                $dislikes = $thumbs_row['dislikes'];
                $entity = $thumbs_row['entity'];
                $thumbs_data[$type][$entity] = caLikes($likes, $dislikes);
            }
            
            
            
        } catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
        }
        
        return $thumbs_data;
    }
    
?>
